package mi.payment.transaction.authorize.complete.revoke;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.TransactionInputPwdOperation;
import mi.payment.transaction.V2InputPasswordActivity;
import mi.payment.ui.base.PageRoute;
import mi.payment.utils.transaction.PreAuthorizationCompleteRevokeTask;
import sunmi.payment.ui.dialog.LoadDialog;

/**
 * 预授权完成撤销模块 -- 输入密码界面
 */
public class PreAuthorizeCompleteRevokeInputPWDActivity extends V2InputPasswordActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        startTransactionProcess();
    }

    private void initView() {
        mTvTitle.setText(R.string.pre_auth_complete_revoke);

        PayDetail originalPay = (PayDetail) getIntent().getSerializableExtra("extra_payDetail");
        PayDetail payDetail = mTradeData.getPayDetail();
        if (originalPay != null) {
            // 赋值金额
            payDetail.amount = originalPay.amount;
            // 赋值原授权号
            payDetail.originalAuthNo = originalPay.authNo;
            // 赋值原交易参考号
            payDetail.originalReferNo = originalPay.referNo;
            // 原交易流水号
            payDetail.originalPOSNum = originalPay.voucherNo;
            // 原交易日期
            payDetail.originalTransDate = originalPay.TradeDate;
            // 原交易批次号
            payDetail.originalBathNo = originalPay.BathNo;
            // IC卡片序列号
            payDetail.cardSerialNo = originalPay.cardSerialNo;
        }

        // 预授权完成撤销是否输入密码
        boolean isFieldAuthCompleteRevokePwd = TransactionInputPwdOperation.getInstance().getTransactionInputPwd().isFieldAuthCompleteRevokePsd();
        setForceInputPWD(isFieldAuthCompleteRevokePwd);
    }

    @Override
    @SuppressLint("StaticFieldLeak")
    public void onStartOnline() {
        // 预授权完成撤销
        mTradeData.getPayDetail().setTransactionType(7);
        PreAuthorizationCompleteRevokeTask task = new PreAuthorizationCompleteRevokeTask(this) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                PageRoute.startAction(mActivity, PageRoute.TYPE_PRE_AUTH_COMPLETE_REVOKE, resPay);
            }

            @Override
            protected void onTaskTimeoutImpl() {
                PageRoute.startTimeoutAction(mActivity, PageRoute.TYPE_PRE_AUTH_COMPLETE_REVOKE, mTradeData.payDetail);
            }

        };
        LoadDialog loadingDialog = getLoadingDialog(R.string.load_connecting);
        task.setLoadDialog(loadingDialog);
        task.execute(mTradeData);
    }

    public static void startAction(Context context, PayDetail payDetail) {
        Intent intent = new Intent(context, PreAuthorizeCompleteRevokeInputPWDActivity.class);
        intent.putExtra("extra_payDetail", payDetail);
        context.startActivity(intent);
    }


}
